function getClick(obj) {    //处理地雷点击事件
    // 处理地雷点击事件
    if (obj.className.search('init') !== -1) {  //如果当前为初始状态
        if (obj.mine) {     //如果该方块是地雷
            //console.log("点到地雷啦")
            //失败了
            gameover(obj);
        }
        else {      //不是地雷
            calculate(obj)
            obj.disabled = true;
            if (document.getElementById('num-box').safeBox === 0) {
                //过关了
                pass();
            }
        }
    }
    //console.log("当前方块周围的地雷数量为：", mine_count(Math.floor(obj.id / 100), obj.id % 100,obj.allNum));
    //console.log('安全方块的数量：', document.getElementById('num-box').safeBox)
}

function getFlag(obj) {     //处理地雷右键事件
    //处理鼠标插旗事件
    if (obj.className.search('init') !== -1) {  //如果当前为初始状态
        //将元素类名设置为flag状态
        obj.className = 'mine flag iconfont icon-qizhi ';
        updateNum(-1);
    }
    else if (obj.className.search('flag') !== -1) {    //为插旗状态
        obj.className = 'mine init iconfont icon-kongbai';
        updateNum(1);
    }
}

function calculate(obj) {   //计算当前方块点击后的状态
    const allNum = MINENUM;
    let mine_stack = new Array(0);     //创建一个待处理的地雷栈
    mine_stack.push(obj);
    //循坏处理方块
    while (mine_stack.length !== 0) {   //只要栈里还有元素
        const temp = mine_stack[0];
        mine_stack.splice(0, 1);    //出栈
        //如果方块是地雷或者已被点击，就跳过
        if (temp.mine || temp.className.search('click') !== -1) continue;
        //如果该方块已被插旗，则返还地雷数量
        if (temp.className.search('flag') !== -1) updateNum(1);

        row = Math.floor(temp.id / 100);
        col = temp.id % 100;
        const mine_num = mine_count(row, col, allNum)   //计算当前方块周围的地雷数量

        if (mine_num > 0) {     //如果地雷数量大于0
            temp.className = `mine click iconfont icon-${mine_num}`
            temp.disabled = true;
        }
        else {  //地雷数量等于0
            temp.className = `mine click iconfont icon-kongbai`
            temp.disabled = true;
            const up = row - 1, left = col - 1, right = col + 1, down = row + 1;
            if (up > 0) {   //上方方块
                const t = document.getElementById(up * 100 + col);
                if (t.className.search('click') === -1) {    //该元素不是已点击状态
                    mine_stack.push(t);
                }
            }
            if (left > 0) {     //左边方块
                const t = document.getElementById(row * 100 + left);
                if (t.className.search('click') === -1) {    //该元素不是已点击状态
                    mine_stack.push(t);
                }
            }
            if (right <= allNum) {     //右边方块
                const t = document.getElementById(row * 100 + right);
                if (t.className.search('click') === -1) {    //该元素不是已点击状态
                    mine_stack.push(t);
                }
            }
            if (down <= allNum) {     //下方方块
                const t = document.getElementById(down * 100 + col);
                if (t.className.search('click') === -1) {    //该元素不是已点击状态
                    mine_stack.push(t);
                }
            }
            if (up > 0 && left > 0) {   //左上方方块
                const t = document.getElementById(up * 100 + left);
                if (t.className.search('click') === -1) {    //该元素不是已点击状态
                    mine_stack.push(t);
                }
            }
            if (up > 0 && right <= allNum) {   //右上方方块
                const t = document.getElementById(up * 100 + right);
                if (t.className.search('click') === -1) {    //该元素不是已点击状态
                    mine_stack.push(t);
                }
            }
            if (down <= allNum && left > 0) {   //左下方方块
                const t = document.getElementById(down * 100 + left);
                if (t.className.search('click') === -1) {    //该元素不是已点击状态
                    mine_stack.push(t);
                }
            }
            if (down <= allNum && right <= allNum) {   //右下方方块
                const t = document.getElementById(down * 100 + right);
                if (t.className.search('click') === -1) {    //该元素不是已点击状态
                    mine_stack.push(t);
                }
            }
        }
        update_safeBox(-1);
    }


}

function createMine(num) {  //创建地雷
    let mine_num = 0;
    switch (num) {  //根据地图大小获得地雷数量
        case 9:
            mine_num = 10;
            break;
        case 16:
            mine_num = 40;
            break;
        case 30:
            mine_num = 189;
            break;
        default:
            mine_num = Math.round((Math.random() * 0.09 + 0.12) * num * num);
            break;
    }
    //更新地雷数量
    updateNum(mine_num);
    update_safeBox(num * num - mine_num);
    //console.log('当前安全方块数量', document.getElementById('num-box').safeBox);
    //创建二维数组
    let arr = new Array(num);
    for (let i = 0; i < num; i++) {
        arr[i] = new Array(num);
    }

    //循环往数组中放入地雷
    let row, col;
    while (mine_num !== 0) {
        row = Math.floor(Math.random() * num) + 1;
        col = Math.floor(Math.random() * num) + 1;
        temp = document.getElementById(row * 100 + col);
        if (arr[row - 1][col - 1] === 1) {    //若随机到的方块有地雷了
            continue;
        }
        arr[row - 1][col - 1] = 1;
        mine_num--;
    }
    return arr;
}

function mine_count(row, col, allNum) {     //计算当前方块周围的地雷数量
    let num = 0;
    for (let i = row - 1; i <= row + 1; i++) {
        if (i < 1 || i > allNum) continue;
        for (let j = col - 1; j <= col + 1; j++) {
            if ((i === row && j === col) || j < 1 || j > allNum) continue;
            if (document.getElementById(i * 100 + j).mine) {
                num++;
            }
        }
    }
    return num;
}

function updateNum(changeNum) {  //更新地雷数量
    let num_box = document.getElementById('num-box');
    num_box.innerText = Number(num_box.innerText) + changeNum;
    //console.log('更新地雷数量啦', num_box);
}

function update_safeBox(changeNum) {  //更新安全方块的数量
    let num_box = document.getElementById('num-box');
    num_box.safeBox = num_box.safeBox + changeNum;
}
